package com.example.shopsystem.control;

import com.example.shopsystem.aop.TokenMethod;
import com.example.shopsystem.entity.Business;
import com.example.shopsystem.service.BusinessServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/business")
public class BusinessRg {
    @Autowired
    BusinessServiceImpl businessService;

    @PostMapping("/busRegister")
    public Result busRegister(@RequestBody Business business) {//注册
        if (business.getPhone() == null || business.getPwd() == null) {
            return Result.phoneNull(null);
        }
        if (business.getPhone().length() != 11 || business.getPwd().length() < 6) {
            return Result.phoneBad(null);
        }
        if (!businessService.register(business)) {
            return Result.intFail(null);
        }
        return Result.ok(null);
    }

    @PostMapping("/busSignIn")
    public Result busSignIn(@RequestBody Business business, HttpServletResponse response) {//登录
        if (business.getPhone() == null || business.getPwd() == null) {
            return Result.phoneNull(null);
        }
        if (!businessService.signIn(business)) {
            return Result.pwdBad(null);
        }
        String type = "bus";
        long exTime = System.currentTimeMillis() + 10000 * 60;
        String org = business.getId() + ";" + business.getPwd() + ";" + exTime + ";" + type;
        //生成token
        String token = TokenMethod.token(org);
        //把token放到头部
        response.setHeader("Token", token);
        return Result.ok(null);
    }
}
